import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';

@Injectable({
  providedIn: 'root'
})

export class MaterialService {
  private part = '/part';
  constructor(private http:HttpClient) { }
  private createPaginationParams(currentPage: number, itemsPerPage: number): HttpParams {
    return new HttpParams()
      .set('page', currentPage.toString())
      .set('pageSize', itemsPerPage.toString());
  }// 创建分页参数

  getAllPartClasses(){
    const url = this.part+'/GetAllPartClasses';
    return this.http.get(environment.apiUrl+ url);
  }// 获取所有物料分类

  getPartClassesWithChildren(page?: number, pageSize?: number){
    let params = new HttpParams();
    if (page !== undefined && pageSize !== undefined) {
      params = this.createPaginationParams(page, pageSize);
    }
    return this.http.get(environment.apiUrl+ this.part+'/GetPagedPartClassesWithChildren',{
      params: params
    })
  }// 获取所有物料分类


  createPartClass(data:any){
    const url = this.part+'/CreatePartClass';
    return this.http.post(environment.apiUrl+ url,data);
  }// 创建物料分类

  getPartClassById(id:number){
    const url = this.part+'/GetPartClassById';
    return this.http.get(environment.apiUrl+ url,{params:{id}});
  }// 获取物料分类详情

  updatePartClass(data:any, id:number){
    const url = this.part+'/UpdatePartClass';
    return this.http.put(environment.apiUrl+ url,data,{params:{id}});
  }// 修改物料分类

  deletePartClass(id:number){
    const url = this.part+'/DeletePartClass';
    return this.http.delete(environment.apiUrl+ url,{params:{id}});
  }// 删除物料分类

  createPart(data:any){
    const url = this.part+'/CreatePart';
    return this.http.post(environment.apiUrl+ url,data);
  }// 创建物料
  updatePart(data:any, id:number){
    const url = this.part+'/UpdatePart';
    return this.http.put(environment.apiUrl+ url,data,{params:{id}});
  }// 修改物料
  deletePart(id:number){
    const url = this.part+'/DeletePart';
    return this.http.delete(environment.apiUrl+ url,{params:{id}});
  }// 删除物料
  deleteParts(id:number[]){
    const url = this.part+'/DeleteParts';
    const options = {
      body: id
    };
    return this.http.delete(environment.apiUrl+ url,options);
  }// 批量删除物料
  getPartById(id:any){
    const url = this.part+'/GetPartById';
    return this.http.get(environment.apiUrl+ url,{params:{id}});
  }// 获取物料详情


  downloadPartClassFileModel(){
    const url = '/Excel/DownloadPartClassFileModel';
    return this.http.get(environment.apiUrl+ url,{responseType: 'blob'});
  }// 下载物料分类模板
  ImportPartClassesFromExcel(file:File){
    const url = '/Excel/ImportPartClassesFromExcel';
    const formData: FormData = new FormData();
    formData.append('file', file, file.name);

    return this.http.post(environment.apiUrl + url, formData);
  }// 导入物料分类
  getParts(partClassId?:number,status?:string,searchTerm?:string,page?: number, pageSize?: number){
    let params = new HttpParams();
    if (partClassId) {
      params = params.set('PartClassId', partClassId.toString());
    }
    if (status) {
      params = params.set('Status', status);
    }
    if (searchTerm) {
      params = params.set('SearchTerm', searchTerm);
    }
    if (page !== undefined && pageSize !== undefined) {
      params = params.set('PageNumber', page.toString());
      params = params.set('PageSize', pageSize.toString());
    }
    return this.http.get(environment.apiUrl+ this.part+'/GetParts',{
      params: params
    })
  }
  uploadAttachment(file:File){
    const url = this.part+'/UploadAttachment';
    const formData = new FormData();
    formData.append('file', file);
    return this.http.post(environment.apiUrl+ url,formData);
  }// 上传附件
  enablePart(id:any){
    const url = this.part+'/EnableParts';
    return this.http.post(environment.apiUrl+ url,id);
  }// 启用物料
  disablePart(id:any){
    const url = this.part+'/EnableParts';
    return this.http.post(environment.apiUrl+ url,id);
  }// 禁用物料
  UpdatePart(id:any,data:any){
    const url = this.part+'/UpdatePart';
    return this.http.put(environment.apiUrl+ url,data,{params:{id}});
  }// 修改物料

  DownloadPartFileModel(){
    const url = '/Excel/DownloadPartFileModel';
    return this.http.get(environment.apiUrl+ url,{responseType: 'blob'});
  }// 下载物料模板
  ImportPartsFromExcel(file: File) {
    const url = '/Excel/ImportPartsFromExcel';
    const formData: FormData = new FormData();
    formData.append('file', file, file.name);

    return this.http.post(environment.apiUrl + url, formData);
  } // 导入物料
  DownloadPartFile(){
    const url = '/Excel/DownloadPartFile';
    return this.http.get(environment.apiUrl+ url,{responseType: 'blob'});
  }
  GetPositionInventory(partNum:any){
    const url = '/Part/GetPositionInventory';
    return this.http.get(environment.apiUrl+ url,{params:{partNum}});
  }// 获取库位库存
}
